package com.zcgame.customview.fresh

import android.view.View
import kotlin.math.abs

/**
 *@author:huangLJ
 *@date:2021/7/14 09:19
 */
interface DownLoader:OnDownListener {
    fun createLoaderView(refreshLayout: PullRefreshLayout): View
    fun getConfig():LoaderConfig

    abstract class LoaderConfig{
        //设置加载偏移量
        abstract fun offsetToLoader(refreshLayout: PullRefreshLayout,loaderView:View):Int
        //设置加载后保持的偏移量
        abstract fun offsetToKeepLoader(refreshLayout: PullRefreshLayout,loaderView: View):Int
        //目标的偏移量
        abstract fun totalDistance(refreshLayout: PullRefreshLayout,loaderView:View):Int
        //判断子控件还能不能向下滑动
        abstract fun contentCanScrollDown(refreshLayout: PullRefreshLayout,contentView:View):Boolean
        //分发处理滑动距离
        abstract fun dispatchTouchMove(refreshLayout: PullRefreshLayout, dy: Float, currentDistance:Int, totalDistance:Int):Int
    }

    class DefaultFootConfig:LoaderConfig(){
        override fun offsetToLoader(refreshLayout: PullRefreshLayout, loaderView: View): Int {
            return (loaderView.measuredHeight * 1.2f).toInt()
        }

        override fun offsetToKeepLoader(refreshLayout: PullRefreshLayout, loaderView: View): Int {
            return loaderView.measuredHeight
        }

        override fun totalDistance(refreshLayout: PullRefreshLayout, loaderView: View): Int {
            return loaderView.measuredHeight * 2
        }

        override fun contentCanScrollDown(refreshLayout: PullRefreshLayout, contentView: View): Boolean {
            return contentView.canScrollVertically(1)
        }

        override fun dispatchTouchMove(refreshLayout: PullRefreshLayout, dy: Float, currentDistance: Int, totalDistance: Int): Int {
            val hDataY = dy /2
            val ps = (-hDataY * abs(currentDistance)) / totalDistance - hDataY
            return (dy + ps * 0.9f).toInt()
        }
    }
}